<?php

namespace App\Http\Controllers\SystemManagement;

use App\Http\Requests\System\UserTask;
use App\Models\Constant\Fields;
use App\Models\PersonInfoManagement\PersonInfo;
use App\Models\SystemManagement\Banci;
use App\Models\SystemManagement\Post;
use App\Models\SystemManagement\UserClasses;
use App\Models\SystemManagement\UserPost;
use App\Models\SystemManagement\UserWorkspace;
use App\Models\SystemManagement\WorkSpace;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

/**
 * Class UserTaskController
 * @package App\Http\Controllers\SystemManagement
 * @group 员工任务分派
 */
class UserTaskController extends Controller
{

    /**
     * 给员工分配任务
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam user_id int required 用户id
     * @bodyParam post_id int required 岗位id
     * @bodyParam classes_id int required 班次id
     * @bodyParam workspace_id int required 作业区域id
     *
     * @response 201 {"msg":"添加成功"}
     */
    public function userTask(Request $request){
        $post = $request->only([Fields::USER_ID,Fields::POST_ID]);
        $classes = $request->only([Fields::USER_ID,Fields::CLASS_ID]);
        $workspace = $request->only([Fields::USER_ID,Fields::WS_ID]);
        try{
            DB::beginTransaction();
            // $this->saveToPersonInfo($request);
            // $this->userToPost($post);
            // $this->userToClasses($classes);
            // $this->userToWorkspace($workspace);
            DB::commit();
            return response()->json(encodeMsg("添加成功",201));
        }catch (\Exception $e){
            DB::rollBack();
            dd($e->getMessage());
            return response()->json(encodeMsg("添加失败",400));
        }
    }

    /**
     * @param $post
     * @return mixed
     * 给员工安排工作岗位
     */
    public function userToPost($post){
        $utp = UserPost::create($post);
        return $utp;
    }

    /**
     * @param $classes
     * @return mixed
     * 给员工安排工作班次
     */
    public function userToClasses($classes){
        $utc = UserClasses::create($classes);
        return $utc;
    }

    /**
     * @param $workspace
     * @return mixed
     * 给员工安排工作区域
     */
    public function userToWorkspace($workspace){
        $utw = UserWorkspace::create($workspace);
        return $utw;
    }

    /**
     * 员工任务存入人员信息表
     * @param $request
     * @return mixed
     */
    public function saveToPersonInfo($request){
        $user_id = $request->input(Fields::USER_ID);
        $post_id = $request->input(Fields::POST_ID);
        $classes_id = $request->input(Fields::CLASS_ID);
        $workspace_id = $request->input(Fields::WS_ID);
        $user = Employee::find($user_id);
        $data['user_id'] = $user->id;
        $data['name'] = $user->name;
        $data['phone'] = $user->phone;
        $data['department_id'] = $user->department_id;

        $post = Post::find($post_id);
        $data['post_id'] = $post->id;
        $data['post'] = $post->name;

        $classes = Banci::find($classes_id);
        $data['classes_id'] = $classes->id;
        $data['classes'] = $classes->name;
        $data['work_time'] = $classes->work_time;

        $workspace = WorkSpace::find($workspace_id);
        $data['workspace_id'] = $workspace->id;
        $data['workspace'] = $workspace->name;

        $pi = PersonInfo::create($data);
        return $pi;
    }
}
